import { createWebHashHistory, createRouter } from "vue-router";
import store from "../store";

import Login from "../page/login/index.vue";
import Register from "../page/register/index.vue";
import Home from "../page/home/index.vue";
import Main from "../page/home/main/index.vue";
import ShuoShuo from "../page/home/shuoshuo/index.vue";
import KongJian from "../page/home/kongjian/index.vue";
import PersonalCenter from "../page/home/personal-center/index.vue";

const routes = [
    {
        path: "/",
        component: Home,
        children: [
            {
                path: "/",
                component: KongJian,
                meta: {
                    requiresAuth: true,
                },
            },
            {
                path: "/main",
                component: Main,
                meta: {
                    requiresAuth: true,
                },
            },
            {
                path: "/shuoshuo",
                component: ShuoShuo,
                meta: {
                    requiresAuth: true,
                },
            },
            {
                path: "/personal-center",
                component: PersonalCenter,
                meta: {
                    requiresAuth: true,
                },
            },
        ],
    },
    {
        path: "/login",
        component: Login,
    },
    {
        path: "/register",
        component: Register,
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const isLogin = store.state.isLogin;
    if (to.meta.requiresAuth && !isLogin) {
        next("/login");
    } else {
        next();
    }
});

export default router;
